// DlgSendFileHtml.cpp : implementation file
//

#include "stdafx.h"
#include "Celsus.h"
#include "DlgSendFileHtml.h"
#include "DlgPreviewHTML.h"
#include "SendMessage.h"


// CDlgSendFileHtml dialog

IMPLEMENT_DYNCREATE(CDlgSendFileHtml, CDHtmlDialog)

CDlgSendFileHtml::CDlgSendFileHtml(CWnd* pParent /*=NULL*/)
	: CDHtmlDialog(CDlgSendFileHtml::IDD, CDlgSendFileHtml::IDH, pParent)
{
	m_nUserID = 0;
	m_nItemID = 0;
	m_nStaffID = 0;
	m_nUserCode = 0;
	m_nLibraryID = 0;
	m_nStatus = -1;
	m_nCollectionID = 0;

	m_bDeferEmail = FALSE;
	m_bDeleteFile = TRUE;
	m_bPreviewEmail = FALSE;
	m_bManual = FALSE;
	m_nHeight = 0;
	m_nWidth = 0;
}

CDlgSendFileHtml::~CDlgSendFileHtml()
{
}

BEGIN_MESSAGE_MAP(CDlgSendFileHtml, CDHtmlDialog)
END_MESSAGE_MAP()

BEGIN_DHTML_EVENT_MAP(CDlgSendFileHtml)
	DHTML_EVENT_ONCLICK(_T("ButtonOK"), OnButtonOK)
	DHTML_EVENT_ONCLICK(_T("ButtonCancel"), OnButtonCancel)
	DHTML_EVENT_ONCLICK(_T("ButtonSend"), OnButtonSend)
	DHTML_EVENT_ONCLICK(_T("ButtonView"), OnButtonView)
	DHTML_EVENT_ONCLICK(_T("ButtonAttach"), OnButtonAttach)
END_DHTML_EVENT_MAP()

void CDlgSendFileHtml::DoDataExchange(CDataExchange* pDX)
{
	CDHtmlDialog::DoDataExchange(pDX);

	DDX_DHtml_ElementInnerText( pDX, _T("Info"), m_szInfo );

	DDX_DHtml_ElementValue( pDX, _T("fid"), m_szFID );
	DDX_DHtml_ElementValue( pDX, _T("File"), m_szFileName );
	DDX_DHtml_ElementValue( pDX, _T("Description"), m_szDescription );	
	DDX_DHtml_ElementValue( pDX, _T("EmailAddress"), m_szTo );

	DDX_DHtml_CheckBox( pDX, _T("DeferEmail"), m_bDeferEmail );
	DDX_DHtml_CheckBox( pDX, _T("DeleteFile"), m_bDeleteFile );
	DDX_DHtml_CheckBox( pDX, _T("PreviewEmail"), m_bPreviewEmail );
}

BOOL CDlgSendFileHtml::OnInitDialog()
{
	CDHtmlDialog::OnInitDialog();

	// use HTML title
	//m_bUseHtmlTitle = TRUE;

	// show scroll bars
	//SetHostFlags( DOCHOSTUIFLAG_NO3DBORDER );

	CString szURL;
	// check for item
	if( m_nItemID > 0 )
		szURL.Format( _T("%s?fid=%.8ld"), appConfig.m_General.szSendHtmlFileLink, m_nItemID );
	else
		szURL = appConfig.m_General.szSendHtmlFileLink;

	// navigate to form
	Navigate( szURL );

	// if not manual get info
	if( !m_bManual )
	{
		// set title
		CString szText;
		szText.Format( _T("Sending file for: %.8ld"), m_nItemID );
		SetWindowText( szText );

		// get request
		REQUEST_SET Request;
		appConfig.m_SQL.GetRequestSet( &Request, m_nItemID );
		// get variables
		m_szRequest = Request.m_request;
		m_szDescription = Request.m_request.Left( DETAIL_LENGTH );
		m_szDateIn = Request.m_date_in.Format( appConfig.m_General.szDateFormat );
		// correct file name
		CorrectFilename( &m_szDescription );
		m_nUserID = Request.m_user_id;

		// get user
		USER_SET User;
		appConfig.m_SQL.GetUserSet( &User, m_nUserID );
		// get variables
		m_szTo = User.m_email;
		m_szInfo.Format( _T("Name: %s\r\nCompany: %s\r\nDate In: %s"), User.m_name, User.m_company, m_szDateIn );
	}
	else
	{
		// set title
		SetWindowText( _T("Manually Sending File") );
	}

	// get library info
	LIBRARY_LOOKUP Library;
	appConfig.m_SQL.GetDescriptionLibrary( &Library, m_nLibraryID );
	m_szFrom = Library.szEmail;
	m_szName = Library.szName;
	m_szSubject = Library.szSubject;

	// set defaults
	m_bDeleteFile = appConfig.m_General.bRemoveFileAfterSend;

	// update data
	UpdateData( FALSE );

	return TRUE;  // return TRUE  unless you set the focus to a control
}

// CDlgSendFileHtml message handlers

HRESULT CDlgSendFileHtml::OnButtonSend(IHTMLElement* /*pElement*/)
{
	TCHAR drive[_MAX_DRIVE];
	TCHAR dir[_MAX_DIR];
	TCHAR fname[_MAX_FNAME];
	TCHAR ext[_MAX_EXT];

	// update data
	UpdateData();

	// check for file name
	if( m_szFileName.IsEmpty() )
	{
		AfxMessageBox( _T("Please select file for request.") );
		return S_FALSE;
	}

	// get message
	m_szMessage = appConfig.ReadFile( appConfig.m_Email.szSendMessageFile );

	// split path
	_tsplitpath_s( m_szFileName, drive, _MAX_DRIVE, dir, _MAX_DIR, fname, _MAX_FNAME, ext, _MAX_EXT );
	// set file name
	m_szFile.Format( _T("%.8ld_%ld%s"), m_nItemID, m_nUserCode, ext );
	
	// format message
	appConfig.FormatMacros( &m_szMessage, m_nItemID );
	// format subject macros
	appConfig.FormatMacros( &m_szSubject, m_nItemID, -1, STATUS_ID_COMPLETE );

	// preview email
	if( m_bPreviewEmail )
	{
		CDlgPreviewHTML dlgPreview;
		dlgPreview.m_bSend = TRUE;
		dlgPreview.m_szHTML = m_szMessage;
		dlgPreview.m_szSubject = m_szSubject;

		// show message
		if( dlgPreview.DoModal() != IDOK )
			return S_FALSE;
		
		// get new message
		m_szMessage = dlgPreview.m_szHTML;
		m_szSubject = dlgPreview.m_szSubject;
	}

	REQUEST_SET Request;
	// get request
	appConfig.m_SQL.GetRequestSet( &Request, m_nItemID );
	// set variables
	Request.m_staff_id = m_nStaffID;
	Request.m_status_id = STATUS_ID_COMPLETE;
	Request.m_file = m_szFile;
	Request.m_date_out = COleDateTime::GetCurrentTime();
	Request.m_touched = TRUE;
	Request.m_retain = 0;
	// update request
	appConfig.m_SQL.SetRequestSet( &Request );

	// check for delete files
	if( m_bDeleteFile )
	{
		// delete file
		if( DeleteFile( m_szFileName ) == 0 )
			AfxMessageBox( _T("Send Dialog: Error deleteing files"), MB_OK );
	}

	// send message
	if( !m_bDeferEmail )
	{
		CSendMessage send;
		if( !send.SendEmail( m_szName, m_szFrom, m_szTo, m_szSubject, m_szMessage, m_szAttach ))
			return S_FALSE;
	}

	// save status
	m_nStatus = Request.m_status_id;

	// return
	return S_OK;
}

HRESULT CDlgSendFileHtml::OnButtonOK(IHTMLElement* /*pElement*/)
{
	OnOK();
	return S_OK;
}

HRESULT CDlgSendFileHtml::OnButtonCancel(IHTMLElement* /*pElement*/)
{
	OnCancel();
	return S_OK;
}

HRESULT CDlgSendFileHtml::OnButtonView(IHTMLElement* /*pElement*/)
{
	return S_OK;
}

HRESULT CDlgSendFileHtml::OnButtonAttach(IHTMLElement* /*pElement*/)
{
	// update data
	UpdateData();

	// get message
	m_szMessage = appConfig.ReadFile( appConfig.m_Email.szAttachMessageFile );

	// format message
	appConfig.FormatMacros( &m_szMessage, m_nItemID );
	// format subject macros
	appConfig.FormatMacros( &m_szSubject, m_nItemID, -1, STATUS_ID_COMPLETE );

	// preview email
	if( m_bPreviewEmail )
	{
		CDlgPreviewHTML dlgPreview;
		dlgPreview.m_bSend = TRUE;
		dlgPreview.m_szHTML = m_szMessage;
		dlgPreview.m_szSubject = m_szSubject;

		// show message
		if( dlgPreview.DoModal() != IDOK )
			return S_FALSE;

		// get new message
		m_szMessage = dlgPreview.m_szHTML;
		m_szSubject = dlgPreview.m_szSubject;
	}

	// set attch path
	m_szAttach += m_szFileName;
	m_szAttach += _T(";");

	REQUEST_SET Request;
	// get request
	appConfig.m_SQL.GetRequestSet( &Request, m_nItemID );
	// set variables
	Request.m_staff_id = m_nStaffID;
	Request.m_status_id = STATUS_ID_COMPLETE;
	Request.m_file = "";
	Request.m_date_out = COleDateTime::GetCurrentTime();
	Request.m_touched = TRUE;
	// update request
	appConfig.m_SQL.SetRequestSet( &Request );

	// check for delete files
	if( m_bDeleteFile )
	{
		// delete file
		if( DeleteFile( m_szFileName ) == 0 )
			AfxMessageBox( _T("Send Dialog: Error deleteing files"), MB_OK );
	}

	// send message
	if( !m_bDeferEmail )
	{
		CSendMessage send;
		if( !send.SendEmail( m_szName, m_szFrom, m_szTo, m_szSubject, m_szMessage, m_szAttach ))
			return S_FALSE;
	}

	// save status
	m_nStatus = Request.m_status_id;

	// close dialog
	OnOK();
	return S_OK;
}

void CDlgSendFileHtml::OnNavigateComplete(LPDISPATCH pDisp, LPCTSTR szUrl)
{
	CDHtmlDialog::OnNavigateComplete(pDisp, szUrl);

	// set window pos
	CString szText;
	HRESULT hr = NULL;
	IHTMLElement* psrcElement;
	VARIANT value;
	// get height
	hr = GetElement( _T("DialogHeight"), &psrcElement );
	if( SUCCEEDED( hr ))
	{
		psrcElement->getAttribute( L"value", 0, &value );
		szText = value.bstrVal;
		m_nHeight = _tstoi( szText );
	}
	// get width
	hr = GetElement( _T("DialogWidth"), &psrcElement );
	if( SUCCEEDED( hr ))
	{
		psrcElement->getAttribute( L"value", 0, &value );
		szText = value.bstrVal;
		m_nWidth = _tstoi( szText );
	}
	// set window pos
	if( m_nHeight > 0 && m_nWidth > 0 )
		SetWindowPos( NULL, NULL, NULL, m_nWidth, m_nHeight, SWP_NOACTIVATE | SWP_NOMOVE | SWP_NOOWNERZORDER );
}

void CDlgSendFileHtml::CorrectFilename(CString* pszFileName)
{
	// remove bad characters
	pszFileName->Remove( '*' );
	pszFileName->Remove( ':' );
	pszFileName->Remove( '?' );
	pszFileName->Remove( '$' );
	pszFileName->Remove( '\'' );
	pszFileName->Remove( '\"' );
	pszFileName->Remove( '/' );
	pszFileName->Remove( '|' );
	pszFileName->Remove( '#' );
	pszFileName->Remove( '<' );
	pszFileName->Remove( '>' );
	pszFileName->Remove( '\r' );
	// replace bad characters with words
	pszFileName->Replace( _T("\n"), _T(" ") );
	pszFileName->Replace( _T("&"), _T("and") );

	// replace latin characters
	for( INT nIndex = 0; nIndex < pszFileName->GetLength(); nIndex++ )
	{
		UINT nChar = 0;
		// convert to ascii
		nChar = appConfig.Convert2Ascii( pszFileName->GetAt( nIndex ));
		if( nChar )
			pszFileName->SetAt( nIndex, nChar );
	}
	// trim
	pszFileName->Trim();
}